package com.zengqingfa.rocketmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 *
 * @fileName: MqDemo01Producer
 * @author: zengqf3
 * @date: 2021-4-2 22:47
 * @description: 生产者
 */
@Component
@Slf4j
public class MqDemo01Producer {

    @Value("${rocketmq.producerGroup.demo01}")
    private String producerGroup;

    @Value("${rocketmq.namesrvAddr}")
    private String namesrvAddr;

    private DefaultMQProducer producer;

    @PostConstruct
    public void init() throws Exception {
        log.info("RocketMq生产者Client初始化,producerGroup={},nameServerAddr={}", producerGroup, namesrvAddr);
        producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setInstanceName("demo01:" + UUID.randomUUID().toString());
        producer.start();
        log.info("生产者Client启动成功");
    }

    public void destroy() {
        if (producer != null) {
            producer.shutdown();
        }
        log.info("RocketMq生产者Client关闭");
    }

    public MQProducer getProducer() {
        return producer;
    }
}
